package com.and.listviewdemo1;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter
{
	
	private ArrayList<Item> listItem;
	private LayoutInflater mInflater;
	Context ctx;
	
	public ListAdapter(Context context, ArrayList<Item> listItem)
	{
		mInflater = LayoutInflater.from(context);
		this.listItem = listItem;
		ctx = context;
	}
	
	static class ViewHolder
	{
		TextView name;
		ImageView icon;
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listItem.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return this.listItem;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		
		if (convertView == null)
		{
			convertView = mInflater.inflate(R.layout.item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.textView1);
			holder.icon = (ImageView) convertView.findViewById(R.id.imageView1);
			convertView.setTag(holder);
		}
		else 
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(position < listItem.size())
		{
			Item item = listItem.get(position);
			int id = Integer.parseInt(item.image);
			Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), id);//.decodeFile(item.image);
			holder.icon.setImageBitmap(bmp);
			holder.name.setText(item.name);
		}
		return convertView;
	}
	

}
